package com.lb.jwitter.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblCTDTMonHoc;
import com.lb.jwitter.service.TblCTDTMonHocService;
import com.lb.jwitter.service.TblCanBo;
import com.lb.jwitter.service.TblCanBoService;
import com.lb.jwitter.service.TblDKMHDotDK;
import com.lb.jwitter.service.TblDKMHDotDKHocVien;
import com.lb.jwitter.service.TblDKMHDotDKHocVienChiTietService;
import com.lb.jwitter.service.TblDKMHDotDKHocVienService;
import com.lb.jwitter.service.TblDKMHDotDKNhom;
import com.lb.jwitter.service.TblDKMHDotDKNhomService;
import com.lb.jwitter.service.TblDKMHDotDKService;
import com.lb.jwitter.service.TblDKMHKyHieu;
import com.lb.jwitter.service.TblDKMHKyHieuService;
import com.lb.jwitter.service.TblMenu;
import com.lb.jwitter.service.TblMenuService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblQuyenMenu;
import com.lb.jwitter.service.TblQuyenMenuService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.service.TblTaiKhoanService;
import com.lb.jwitter.service.TblThongTinHocVien;
import com.lb.jwitter.service.TblThongTinHocVienService;
import com.lb.jwitter.service.TblThongTinMonHoc;
import com.lb.jwitter.service.TblThongTinMonHocService;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.ExcelToJSon;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

@ContextConfiguration(locations="classpath:beans.xml") 
public class TblDKMHDotDKHocVienChiTietSaveFromXLSAction {


	private TblTaiKhoan taiKhoanDangNhap; 
	  
	  public TblTaiKhoan getTaiKhoanDangNhap() { 
	      return taiKhoanDangNhap; 
	  } 
	  public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
	      this.taiKhoanDangNhap = taiKhoanDangNhap; 
	  } 
	  
	  private int dotDKId;
	  
	public int getDotDKId() {
		return dotDKId;
	}
	public void setDotDKId(int dotDKId) {
		this.dotDKId = dotDKId;
	}
	
	private String ngayBatDau;
	private String ngayKetThuc;
	private String quyen;

	
	public String getQuyen() {
		return quyen;
	}
	public void setQuyen(String quyen) {
		this.quyen = quyen;
	}
	public String getNgayBatDau() {
		return ngayBatDau;
	}

	public void setNgayBatDau(String ngayBatDau) {
		this.ngayBatDau = ngayBatDau;
	}

	public String getNgayKetThuc() {
		return ngayKetThuc;
	}

	public void setNgayKetThuc(String ngayKetThuc) {
		this.ngayKetThuc = ngayKetThuc;
	}

	private String maNhom;
	public String getMaNhom() {
		return maNhom;
	}
	public void setMaNhom(String maNhom) {
		this.maNhom = maNhom;
	}
	public String getTenNhom() {
		return tenNhom;
	}
	public void setTenNhom(String tenNhom) {
		this.tenNhom = tenNhom;
	}

	private String tenNhom;

	@Autowired // Injected by Spring 
	ApplicationContextProvider contextProvider; 
	 	 
	private Message msg;
	
	  public Message getMsg() {
		return msg;
	}

	public void setMsg(Message msg) {
		this.msg = msg;
	}

	private TblDKMHDotDKHocVienService tblDKMHDotDKHocVienService; 
	
	private JSONArray jsons;

	public JSONArray getJsons() {
		return jsons;
	}
	public void setJsons(JSONArray jsons) {
		this.jsons = jsons;
	}
	
	private String destFile;
	public String getDestFile() {
		return destFile;
	}
	public void setDestFile(String destFile) {
		this.destFile = destFile;
	}
	
	private String result;
	
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public String execute() throws Exception
	{
		Date startDate = new Date();

	     try{ 	
	    	 
	         Map session = ActionContext.getContext().getSession();
	         taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
	         if( taiKhoanDangNhap == null) return "noprivilege";
	         
	        ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
	        
	        TblDKMHDotDKHocVienChiTietService tblDKMHDotDKHocVienChiTietService = (TblDKMHDotDKHocVienChiTietService) context.getBean("tbl_DKMH_DotDK_HocVien_ChiTietClient");
	        TblDKMHDotDKNhomService tblDKMHDotDKNhomService = (TblDKMHDotDKNhomService) context.getBean("tbl_DKMH_DotDK_NhomClient");
	        TblDKMHDotDKService tblDKMHDotDKService = (TblDKMHDotDKService) context.getBean("tbl_DKMH_DotDKClient");
	        
	        jsons = ExcelToJSon.execute(destFile);
	        
	        TblDKMHDotDKNhom nhomDK = new TblDKMHDotDKNhom();
	        nhomDK.setMa(maNhom);
	        nhomDK.setTen(tenNhom);
	        
	        DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
	        Date dd = df.parse(ngayBatDau); 
	        GregorianCalendar c = new GregorianCalendar(); 
	        c.setTime(dd);
	        XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
	        nhomDK.setNgayBatDau(d);
	        
	        dd = df.parse(ngayKetThuc); 
	        c.setTime(dd);
	        d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
	        nhomDK.setNgayKetThuc(d);
	        
	        nhomDK.setQuyen(quyen);
	        nhomDK.setDotDKId(tblDKMHDotDKService.gettblDKMHDotDK(new Long(dotDKId)));
	        
	        Long nhomDKId = tblDKMHDotDKNhomService.createtblDKMHDotDKNhom(nhomDK);
	        nhomDK.setId(nhomDKId);
	        
	        result = tblDKMHDotDKHocVienChiTietService.createListtblDKMHDotDKHocVienChiTietByJson(dotDKId, jsons.toString(), ngayBatDau, ngayKetThuc, quyen, taiKhoanDangNhap, nhomDK.getId().intValue());
	        
	 
	            return Action.SUCCESS; 
	     } 
	     catch(Exception e){ 
	             System.out.println("Loi add lich trinh dang ky from  excell Action"); 
	             e.printStackTrace(); 
	             return Action.ERROR; 
	     } 

	}
	
	
	
	
	
}
